
cd ../HEGIS
do HEGSCNTY_r.do
cd ../code

use ../data/county1980.dta
merge 1:1 statefip countyfip using ../HEGIS/HGISCNTY_r.dta
list statefip countyfip state county nfips if _merge!=3
foreach v in pub2 pub4 avgpub2_e avgpub4_e all{
	replace `v'=0 if _merge==1
}
drop if _merge==2
drop _merge

merge m:1 statefip using ../data/state_unemployment.dta
assert _merge==3
drop _merge

// BEA income data
gen temp = countyfip
merge 1:1 statefip countyfip using ../data/BEA_VA.dta, keep(1 3) // VA counties are coded differently in BEA data
replace countyfip = bea_code if _merge==3
drop _merge
replace countyfip = 188 if statefip==2 & countyfip==140 // Kobuk
replace countyfip = 158 if statefip==2 & countyfip==270 // Wade Hampton -> Kusilvak
replace countyfip = 86 if statefip==12 & countyfip==25 // Miami-Dade
replace countyfip = 901 if statefip==15 & (countyfip==5|countyfip==9) // Kalawao & Maui
replace countyfip = 67 if statefip==30 & countyfip==113 // Yellowstone NP to Park Co. 
replace countyfip = 102 if statefip==46 & countyfip==113 // Shannon -> Oglala
merge m:1 statefip countyfip using ../data/BEA_income.dta, keep(1 3)
assert _merge==3
replace countyfip = temp
drop temp _merge

// urban status
gen temp = county
drop county
gen county = statefip*1000 + countyfip
replace county = 2013 if county==2010 // Aleutian Islands
replace county = 2188 if county==2140 // Kobuk
merge 1:1 county using ../data/county_ua.dta, keep(1 3)
assert _merge==3
drop _merge
drop county
gen county = temp
drop temp

// county coordinates
gen temp = countyfip
replace countyfip = statefip*1000 + countyfip
replace countyfip = 2013 if countyfip==2010 // Aleutian Islands
replace countyfip = 2188 if countyfip==2140 // Kobuk
replace countyfip = 2282 if countyfip==2231 // Yakutat
replace countyfip = 12086 if countyfip==12025 // Miami-Dade
replace countyfip = 30067 if countyfip==30113 // Yellowstone NP to Park Co. 
replace countyfip = 51083 if countyfip==51780 // Sounth Boston to Halifax
merge m:1 countyfip using ../data/county_coordinates.dta, keep(1 3)
assert _merge==3
drop _merge
replace countyfip = temp
drop temp

// in radian 
gen radlo = 2*_pi*(intptlon/360)
gen radla = 2*_pi*(intptlat/360)

local NN = _N
gen dist2 = 0
gen dist4 = 0
gen tui2_c = avgpub2_e
gen tui4_c = avgpub4_e
qui forvalues i=1/`NN'{
	foreach y in 2 4{
		if ( pub`y'[`i']==0 ){
			gen t1 = 39.5*acos( sin(radla)*sin(radla[`i'])+cos(radla)*cos(radla[`i'])*cos(radlo-radlo[`i']) ) if pub`y'==1 & statefip==statefip[`i']
			egen t2 = min(t1)
			gen t3 = cond(!missing(t1) & t2==t1,avgpub`y'_e,0)
			egen t4 = max(t3)
			replace dist`y' = t2 in `i'
			replace tui`y'_c = t4 in `i'
			drop t1 t2 t3 t4
		}
	}
}
// No 2-year college in SD DC 
tab statefip if missing(dist2), m
tab statefip if missing(dist4), m
replace tui2_c = tui4_c if missing(dist2)
replace dist2 = dist4 if missing(dist2)

local vlist statefip countyfip regiona divisiona pub2 pub4 dist2 dist4 uafrac uapop tui2_c tui4_c
forvalues y=1974/1981{
	local vlist `vlist' urate`y' earn_pe`y'
}

sort statefip countyfip
keep `vlist'
order `vlist'
compress
save ../export/geo_info.dta, replace
